//
//  IMSelectFileCell.m
//  wanzhaoIM
//
//  Created by wanzhao on 15/7/16.
//  Copyright (c) 2015年 liuyuantao. All rights reserved.
//

#import "IMSelectFileCell.h"
#import "CFileModel.h"
#import "UIUtil.h"

@interface IMSelectFileCell ()

@property (nonatomic, weak) UIView *lineView;
@property (nonatomic, weak) UILabel *fileLabel;

@end
@implementation IMSelectFileCell

+ (instancetype)imSelectFileCellWithTableView:(UITableView *)tableView
{
    static NSString *cellId = @"IMSelectFileCell";
    IMSelectFileCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
    if (cell == nil) {
        cell = [[IMSelectFileCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellId];
        cell.textLabel.textColor = [UIColor grayColor];
        cell.textLabel.font = [UIFont systemFontOfSize:14.0f];
        cell.textLabel.numberOfLines = 0;
        cell.accessoryType = UITableViewCellAccessoryNone;
    }
    return cell;
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        self.contentView.backgroundColor = ClearColor;
        self.backgroundColor = ClearColor;
        UIView *view = [[UIView alloc] init];
        view.backgroundColor = RGBCOLOR(212, 212, 212);
        [self.contentView addSubview:view];
        self.lineView = view;
        
        
        UILabel *fileLabel = [[UILabel alloc] init];
        fileLabel.textColor = [UIColor grayColor];
        fileLabel.font = [UIFont systemFontOfSize:14.0f];
        fileLabel.numberOfLines = 0;
        [self.contentView addSubview:fileLabel];
        self.fileLabel = fileLabel;
        

    }
    return self;
}
- (void)setFileModel:(CFileModel *)fileModel
{
    _fileModel = fileModel;
    
    self.fileLabel.text = fileModel.FileName;
    NSString *nameStr = [fileModel.FileName lowercaseString];
    if (fileModel.isDir) {
        self.imageView.image = [UIImage imageNamed:@"cool_folders_01"];
    } else {
        
        NSString *fileIcon = [UIUtil getFileIconByName:nameStr];
        if ([fileIcon isEqualToString:@"othersIcon"] && fileIcon != nil) {
            self.imageView.image = [UIImage imageNamed:fileIcon];
            
        } else {
            self.imageView.image = [UIImage imageNamed:fileIcon];
        }

    }
    if (fileModel.isHasSelected) {
        self.accessoryType = UITableViewCellAccessoryCheckmark;
    } else {
        self.accessoryType = UITableViewCellAccessoryNone;
    }

}

- (void)layoutSubviews
{
    [super layoutSubviews];
    
    self.lineView.frame = CGRectMake(15, self.frame.size.height - 0.5, self.frame.size.width - 30, 0.5);
    self.fileLabel.frame = CGRectMake(CGRectGetMaxX(self.imageView.frame) + 10, 5, self.frame.size.width - CGRectGetMaxX(self.imageView.frame) - 20, self.frame.size.height - 10);
}
@end
